<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>百度网盘秒传链接生成</title>
    <meta name="description" content="百度网盘秒传链接在线转存/生成/转换，支持iOS/Android/PC and more">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/5.2.0/css/bootstrap.min.css">
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 text-center">
          <h1>百度网盘秒传链接转换</h1>
          <p id="version">v1.5 mengzonefire version</p>
          <p><b>请先阅读 <a href="./manual.html" target="_blank">使用教程</b></a><br></p>
          <p><a href="./index.html">秒传转存</a> <a href="./multisave.html">批量转存</a> <a href="./gen.html">秒传生成</a></p>
        </div>
        <div class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 text-center">
        </div>
        <div id="body" class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 text-center">
          <div class="form-group">
            <label for="link">秒传链接</label>
            <p><textarea name="link" id="link" class="form-control" lder="需要转换的链接" rows="8"></textarea></p>
            <div class="btn-group" role="group">
              <input type="radio" class="btn-check" name="btnradio" id="pcs" autocomplete="off" checked>
              <label class="btn btn-outline-primary" for="pcs">BaiduPCS-Go</label>

              <input type="radio" class="btn-check" name="btnradio" id="bdpan" autocomplete="off">
              <label class="btn btn-outline-primary" for="bdpan">PanDownload</label>

              <input type="radio" class="btn-check" name="btnradio" id="mengji" autocomplete="off">
              <label class="btn btn-outline-primary" for="mengji">梦姬</label>
            </div>
            <p><button class="btn btn-primary" onclick="convert()">转换</button>
            <p><textarea name="convert" id="convert" class="form-control" placeholder="转换后的链接" rows="8"></textarea></p>
          </div>
          <p><b>所有请求在用户端进行，服务器不保存任何数据</b></p>
        </div>
      </div>
    </div>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/5.2.0/js/bootstrap.min.js"></script>
    <script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
    <script>
      function atou(str) {
        return decodeURIComponent(escape(window.atob(str)));
      }
      function utoa(str) {
        return window.btoa(unescape(encodeURIComponent(str)));
      }
      function Trim(str) {
        return str.replace(/(^\s*)|(\s*$)/gu, '');
      }
      function getLink(link, backcall) {
        const bdpan = link.match(/bdpan:\/\/(.+)/u);
        const pcs = link.match('BaiduPCS-Go');
        const mengji = link.match(/^[\da-f]{32}#[\da-f]{32}#[\d]+#[\s\S]+/iu);
        const mengji_short = link.match(/^[\da-z]{32}#[\d]+#[\s\S]+/iu);
        if (bdpan !== null) {
          const de_b64 = atou(bdpan[1]);
          const md5 = de_b64.match(/\|(.{32})\|/u)[1];
          const slicemd5 = de_b64.match(/\|([^|]{32})$/u)[1];
          const file_length = de_b64.match(/\|([0-9]+)\|/u)[1];
          const file_name = de_b64.match(/^(.+?)\|/u)[1];
          backcall(md5, slicemd5, file_length, file_name);
        }
        else if (pcs) {
          const input = link;
          const length = input.match(/length=([0-9]+)/u)[1];
          const md5 = input.match(/-md5=(.{32})/u)[1];
          const slicemd5 = input.match(/-slicemd5=(.{32})/u)[1];
          const file_name = input.match(/"(.+)"/u)[1];
          backcall(md5, slicemd5, length, file_name);
        }
        else if (mengji) {
          const input = link;
          const md5 = input.match(/^(.{32})#/u)[1];
          const slicemd5 = input.match(/#(.{32})#/u)[1];
          const file_length = input.match(/#([0-9]+)#/u)[1];
          let file_name = input.match(/#[0-9]+#(.+)$/u)[1];
          file_name = Trim(file_name);
          backcall(md5, slicemd5, file_length, file_name);
        }
        else if(mengji_short)
          $('#convert').append("此行检测到简化版标准码, 不支持转换\n")
        else
          $('#convert').append("此行未检测到有效的秒传链接\n")
      }
      function convertpcs(md5, slicemd5, file_length, file_name) {
        return `BaiduPCS-Go rapidupload -length=${file_length} -md5=${md5} -slicemd5=${slicemd5} "/${file_name}"`;
      }
      function convert() {
        $('#convert').html('');
        const lines = $('#link').val().split('\n');
        for (let i = 0; i < lines.length; i++) {
          // code here using lines[i] which will give you each line
          if ($('#pcs').prop('checked')) {
            getLink(lines[i], backcall = function (md5, slicemd5, file_length, file_name) {
              $('#convert').append(`BaiduPCS-Go rapidupload -length=${file_length} -md5=${md5} -slicemd5=${slicemd5} "/${file_name}"\n`);
            });
          }
          if ($('#bdpan').prop('checked')) {
            getLink(lines[i], backcall = function (md5, slicemd5, file_length, file_name) {
              $('#convert').append(`bdpan://${utoa(`${file_name}|${file_length}|${md5}|${slicemd5}`)}\n`);
            });
          }
          if ($('#mengji').prop('checked')) {
            getLink(lines[i], backcall = function (md5, slicemd5, file_length, file_name) {
              $('#convert').append(`${md5}#${slicemd5}#${file_length}#${file_name}\n`);
            });
          }
        }
      }
    </script>
  </body>
</html>
